﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;
using System.Linq.Dynamic;
using WXYW.BLL;
using WXYW.DAL;
using System.Collections;

namespace WXYW
{
    public partial class ReturnToFactoryListConfirmProvider : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindForm();
            }
        }

        private void BindForm()
        {
            try
            {
                ReturnToFactoryService service = new ReturnToFactoryService();
                returnToFactoryFormsGridView.DataSource = service.GetReturnToFactoryFormByStatus("返厂-待确认厂商", ContextService.CurrentAccount);
                returnToFactoryFormsGridView.DataBind();
            }
            catch (Exception ex)
            {
                LogUtility.LogUtilityIntance.LogException(ex, "ReturnToFactoryListConfirmProvider::BindForm");
                ClientScript.RegisterStartupScript(this.GetType(), "tip", string.Format("alert('信息绑定发生错误，请截图保存该错误信息并联系管理员！错误原因：{0}');", ex.Message), true);
            }
        }
        
        protected void rtfLinkButton_Click(object sender, EventArgs e)
        {
            string returnToFactoryID = ((LinkButton)sender).CommandArgument;

            Response.Redirect(string.Format("ReturnToFactoryConfirmProvider.aspx?{0}={1}", Constants.QUERYSTR_RETURNTOFACTORY_ID, returnToFactoryID));
        }

        protected void returnToFactoryFormsGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (e.NewPageIndex >= 0 && e.NewPageIndex < returnToFactoryFormsGridView.PageCount)
            {
                returnToFactoryFormsGridView.PageIndex = e.NewPageIndex;
            }
            
            BindForm();
        }

        protected void returnToFactoryFormsGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
            }

            if (e.Row.RowType == DataControlRowType.Pager)
            {
                DropDownList pageDDL = e.Row.FindControl("pageDDL") as DropDownList;
                for (int index = 1; index <= returnToFactoryFormsGridView.PageCount; index++)
                {
                    pageDDL.Items.Add(index.ToString());
                }
                pageDDL.SelectedValue = (returnToFactoryFormsGridView.PageIndex + 1).ToString();
            }
        }

        protected void returnToFactoryFormsGridView_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                ReturnToFactoryService service = new ReturnToFactoryService();
                IQueryable<ReturnToFactoryForm> forms = service.GetReturnToFactoryFormByStatus("返厂-待确认厂商", ContextService.CurrentAccount).AsQueryable();
                string sortDirection = "asc";
                if (Session["OrderDirection"] != null)
                {
                    sortDirection = Session["OrderDirection"].ToString();
                    if (sortDirection.Equals("asc"))
                    {
                        Session["OrderDirection"] = "desc";
                    }
                    else
                    {
                        Session["OrderDirection"] = "asc";
                    }
                }
                else
                {
                    Session["OrderDirection"] = "asc";
                }
                returnToFactoryFormsGridView.DataSource = forms.OrderBy(string.Format("{0} {1}", e.SortExpression, sortDirection)).ToList();
                returnToFactoryFormsGridView.DataBind();
            }
            catch (Exception ex)
            {
                LogUtility.LogUtilityIntance.LogException(ex, "ReturnToFactoryListConfirmProvider::returnToFactoryFormsGridView_Sorting");
                throw ex;
            }
        }

        protected void pageDDL_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList pageDDL = sender as DropDownList;
            returnToFactoryFormsGridView.PageIndex = Convert.ToInt32(pageDDL.SelectedIndex);
            BindForm();
        }

        protected void returnToFactoryFormsGridView_DataBound(object sender, EventArgs e)
        {
            if (returnToFactoryFormsGridView.BottomPagerRow != null)
            {
                returnToFactoryFormsGridView.BottomPagerRow.Visible = true;
            }
        }

        protected IEnumerable GridView_MustAddARow(IEnumerable data)
        {
            List<ReturnToFactoryForm> dds = (List<ReturnToFactoryForm>)data;
            dds.Add(new ReturnToFactoryForm());
            return dds;
        }
    }
}
